import 'package:flutter/material.dart';

/*使用 InheritedWidget 时涉及到的工作量主要有 2 部分：
创建一个继承自 InheritedWidget 的类，并将其插入 Widget 树
通过 BuildContext 对象提供的 inheritFromWidgetOfExactType 方法查找 Widget 树中特定类型的 InheritedWidget 类的实例
 */
//4.组合
class InheritedWidgetContainer extends StatefulWidget {
  @override
  _InheritedWidgetContainerState createState() =>
      new _InheritedWidgetContainerState();
}

class _InheritedWidgetContainerState extends State<InheritedWidgetContainer> {
  late InheritedModel inheritedModel;

  _initData() {
    inheritedModel = InheritedModel(0);
  }

  @override
  void initState() {
    _initData();
    super.initState();
  }

  _incrementCount() {
    setState(() {
      inheritedModel = new InheritedModel(inheritedModel.count + 1);
    });
  }

  _reduceCount() {
    setState(() {
      inheritedModel = new InheritedModel(inheritedModel.count - 1);
    });
  }

  @override
  Widget build(BuildContext context) {
    return new InheritedContext(
        inheritedModel: inheritedModel,
        increment: _incrementCount,
        reduce: _reduceCount,
        child: new Scaffold(
          appBar: new AppBar(
            title: new Text('InheritedWidget传递数据'),
          ),
          body: Column(
            children: <Widget>[
              new Padding(
                padding:
                    const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
                child: new Text(
                  '我们常使用的\nTheme.of(context).textTheme\nMediaQuery.of(context).size等\n就是通过InheritedWidget实现的',
                  style: new TextStyle(fontSize: 18.0, color: Colors.orange),
                ),
              ),
              new IncrementWidget(),
              new TextWidget(),
              new ReduceWidget(),
            ],
          ),
        ));
  }
}

//3.把 "+" ,“-”, "value" 3个widget隔离
class IncrementWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final inheritedContext = InheritedContext.of(context);
    return Padding(
      padding: const EdgeInsets.all(5),
      child: ElevatedButton(
        onPressed: inheritedContext!.increment,
        // textColor: Colors.black,
        child: Text('+'),
      ),
    );
  }
}

class TextWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final inheritedContext = InheritedContext.of(context);
    return Padding(
      padding: const EdgeInsets.all(5),
      child: Text(
        '当前count:${inheritedContext!.inheritedModel.count}',
        style: TextStyle(fontSize: 18),
      ),
    );
  }
}

class ReduceWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final inheritedContext = InheritedContext.of(context);
    return Padding(
      padding: const EdgeInsets.all(5),
      child: ElevatedButton(
        onPressed: inheritedContext!.reduce,
        // textColor: Colors.black,
        child: Text('-'),
      ),
    );
  }
}

//2.创建一个InheritedWidget
/*
每次当 InheritedElement 的实例更新时会执行该方法并传入更新之前对应的 Widget 对象，
如果该方法返回 true 那么依赖该 Widget 的(在 build 阶段通过 inheritFromWidgetOfExactType 方法查找过该 Widget 的子 widget)实例会被通知进行更新；
如果返回 false 则不会通知依赖项更新。
 */
class InheritedContext extends InheritedWidget {
  //数据
  final InheritedModel inheritedModel;

  //点击+号的方法
  final Function() increment;

  //点击-号的方法
  final Function() reduce;

  InheritedContext({
    Key? key,
    required this.inheritedModel,
    required this.increment,
    required this.reduce,
    required Widget child,
  }) : super(key: key, child: child);

  static InheritedContext? of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType(aspect: InheritedContext);
  }

  @override
  bool updateShouldNotify(InheritedContext oldWidget) {
    return inheritedModel != oldWidget.inheritedModel;
  }
}

//1.创建一个model，用于保存当前的计数值
class InheritedModel {
  final int count;

  const InheritedModel(this.count);
}
